#include<bits/stdc++.h>
#define ll long long
using namespace std;
int dfn[200005],rk[200005],tot,son[200005],siz[200005],fa[200005],dep[200005],top[200005],n,q,bz[200005][18];
ll sum[200005],a,b,c;
vector<pair<ll,ll> > edge[200005];


struct fastin{
	char buf[1<<24],*bg=NULL,*ed=NULL;
	char getchar(){
		if(bg==ed)ed=(bg=buf)+fread(buf,1,sizeof(buf),stdin);
		return (bg!=ed?*(bg++):EOF);
	}
	fastin& operator>>(int& x){
		x=0;
		char c=getchar();
		while(c<'0'||c>'9')c=getchar();
		while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
		return *this;
	}
	fastin& operator>>(ll& x){
		x=0;
		char c=getchar();
		while(c<'0'||c>'9')c=getchar();
		while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
		return *this;
	}
	void tie(int x){}
}fstin;
#define cin fstin
void dfs1(int u,int f){
	fa[u]=f;
	dep[u]=dep[f]+1;
	siz[u]=1;
	for(auto i:edge[u])if(i.first!=f){
		dfs1(i.first,u);
		siz[u]+=siz[i.first];
		if(siz[i.first]>siz[son[u]])son[u]=i.first;
	}
}
void dfs2(int u,int tp){
	dfn[u]=++tot;
	rk[tot]=u;
	top[u]=tp;
	if(son[u])dfs2(son[u],tp);
	for(auto i:edge[u])if(i.first!=fa[u]&&i.first!=son[u])dfs2(i.first,i.first);
}
int LCA(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		u=fa[top[u]];
	}
	if(dep[u]<dep[v])swap(u,v);
	return v;
}
ll lcasum(int u,int v){
	ll re=0;
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		re+=sum[dfn[u]]-sum[dfn[top[u]]-1];
		u=fa[top[u]];
	}
	if(dep[u]<dep[v])swap(u,v);
	re+=sum[dfn[u]]-sum[dfn[v]];
	return re;
}
struct lca_tree{
	int tree[800005];
	void build(int i,int l,int r){
		if(l==r){
			tree[i]=l;
			return;
		}
		int mid=(l+r)>>1;
		build(i<<1,l,mid);
		build(i<<1|1,mid+1,r);
		tree[i]=LCA(tree[i<<1],tree[i<<1|1]);
	}
	int lca(int i,int l,int r,int tl,int tr){
		if(l<=tl&&tr<=r)return tree[i];
		int mid=(tl+tr)>>1;
		if(l<=mid&&r>mid)return LCA(lca(i<<1,l,r,tl,mid),lca(i<<1|1,l,r,mid+1,tr));
		if(l<=mid)return lca(i<<1,l,r,tl,mid);
		return lca(i<<1|1,l,r,mid+1,tr);
	}
}tree1;
struct node{
	node *l=NULL,*r=NULL;
	int sum=0;
}*tree[200005],allnd[200005*64];
int node_top=0;
node* getnode(){return &allnd[++node_top];}
node* getnode(node& x){allnd[++node_top]=x;return &allnd[node_top];}
void insert(node*& i,int l,int r,int u){
	if(i)i=getnode(*i);
	else i=getnode();
	i->sum++;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(u<=mid)insert(i->l,l,mid,u);
	else insert(i->r,mid+1,r,u);
}
int getsum(node* i,int l,int r,int tl,int tr){
	if(i==NULL)return 0;
	if(l<=tl&&tr<=r)return i->sum;
	int mid=(tl+tr)>>1,re=0;
	if(l<=mid)re+=getsum(i->l,l,r,tl,mid);
	if(r>mid)re+=getsum(i->r,l,r,mid+1,tr);
	return re;
}

int getzs(int u,int l,int r){
	return getsum(tree[dfn[u]+siz[u]-1],l,r,1,n)-getsum(tree[dfn[u]-1],l,r,1,n);
}
int used=0,bgt;
int nogj(int u,int l,int r){
	for(int i=17;~i;i--)if(bz[u][i]&&!getzs(bz[u][i],l,r))u=bz[u][i];
	return fa[u];
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
  	freopen("memory.in","r",stdin);
  	freopen("memory.out","w",stdout);
  	
	cin>>n>>q;
	for(int i=1;i<n;i++){
		cin>>a>>b>>c;
		edge[a].emplace_back(b,c);
		edge[b].emplace_back(a,c);
	}
	dfs1(1,0);
	dfs2(1,1);
	for(int i=1;i<=n;i++){
		for(auto j:edge[i])if(j.first!=fa[i])sum[dfn[j.first]]=j.second;
		bz[i][0]=fa[i];
	}
	tree1.build(1,1,n);
	for(int i=1;i<=n;i++){
		tree[i]=tree[i-1];
		insert(tree[i],1,n,rk[i]);
	}
	for(int i=1;i<18;i++){
		for(int j=1;j<=n;j++)bz[j][i]=bz[bz[j][i-1]][i-1];
	}
	for(int i=1;i<=n;i++)sum[i]+=sum[i-1];
	
	ll lstans=0;
	while(q--){
		cin>>a>>b>>c;
		a^=lstans,b^=lstans,c^=lstans;
		int the_lca=tree1.lca(1,b,c,1,n);
		if(LCA(the_lca,a)==a)lstans=lcasum(the_lca,a);
		else{
			if(getzs(a,b,c))lstans=0;
			else{
//				bgt=clock();
				int gg=nogj(a,b,c);
//				used+=clock()-bgt;
				if(gg==the_lca)lstans=lcasum(the_lca,a);
				else{
					if(LCA(gg,the_lca)==gg)lstans=lcasum(the_lca,a);
					else lstans=lcasum(gg,a);
				}
			}
		}
		cout<<lstans<<'\n';
	}
//	cerr<<used/(double)CLOCKS_PER_SEC;
}
